var express = require('express');
var router = express.Router();

var crypto = require('crypto');
var User = require('../modules/db').userSchema;
var Blog = require('../modules/db').blogSchema;

/* GET home page. */
router.get('/', function(req, res) {
	Blog.find().sort('-date').limit(10).exec(function(err,blogs){
		if(req.session.user)
			return res.render('user',{title:'寻寻觅觅',user:req.session.user,blogs:blogs});
		res.render('index', { title: '寻寻觅觅',blogs:blogs });
	});
});

router.post('/',function(req,res){
	User.findOne({username:req.body.username},function(err,user){
		var md5 = crypto.createHash('md5'),
		password = md5.update(req.body.password).digest('hex');
		if(user && user.password == password){
			req.session.user = user;
			req.session.sucess = "log success";
		Blog.find().sort('-date').limit(10).exec(function(err,blogs){
			if(req.session.user)
				return res.render('user',{title:'寻寻觅觅',user:req.session.user,blogs:blogs});
			res.render('index', { title: '寻寻觅觅',blogs:blogs });
		});
		}else{
			req.session.err="用户名不存在或密码错误";
			res.redirect('/err');
		}
	});	
	
	
	
})

router.get('/reg',function(req,res){
	res.render('reg',{title:"注册"});
});

router.post('/reg',function(req,res){
	User.findOne({username:req.body.username},function(err,user){
		if(err)
			console.log(err);
		if(user){
			req.session.err="用户名已存在！";
			return res.redirect('/err');
		}
		if(req.body.passowd != req.body.re_passowd){
			req.session.err="两次密码不一致";
			return res.res('/err');
		}
		var md5 = crypto.createHash('md5'),
		password = md5.update(req.body.password).digest('hex');
		user = new User({
			password:password,
			username:req.body.username
		});
		user.save();
		req.session.user = user;
		Blog.find().sort('-date').limit(10).exec(function(err,blogs){
			if(req.session.user)
				return res.render('user',{title:'寻寻觅觅',user:req.session.user,blogs:blogs});
			res.render('index', { title: '寻寻觅觅',blogs:blogs });
		});
	});
});

router.get('/err',function(req,res){
	res.render('err',{
		title:'err',
		err:req.session.err
	});
});

router.get('/logout',function(req,res){
	req.session.user = null;
		Blog.find().sort('-date').limit(10).exec(function(err,blogs){
			if(req.session.user)
				return res.render('user',{title:'寻寻觅觅',user:req.session.user,blogs:blogs});
			res.render('index', { title: '寻寻觅觅',blogs:blogs });
		});
});

router.post('/post_blog',function(req,res){
	var name;
	if(req.session.user)
		name = req.session.user.username;
	else name="匿名用户";
		var blog = new Blog({
			username:name,
			title:req.body.title,
			content:req.body.content,
			date:new Date(),
		});
	blog.save(function(){
		Blog.find().sort('-date').limit(10).exec(function(err,blogs){
			if(req.session.user)
				return res.render('user',{title:'寻寻觅觅',user:req.session.user,blogs:blogs});
			res.render('index', { title: '寻寻觅觅',blogs:blogs });
		});
	});
});

router.get('/blog/:blog_id',function(req,res){
	Blog.findOne({_id:req.param('blog_id')},function(err,blog){
		console.log(blog);
		if(req.session.user)
			return res.render('user_blog',{title:blog.title,user:req.session.user,blog:blog});
		res.render('no_blog', { title: blog.title,blog:blog });	
	});
});





function cheak_login(req,res,next){
	if(!req.session.user){
		res.redirect("/err");
		res.session.err="您尚未登录！";
	}
	next();
}
function cheak_not_login(req,res,next){
	if(req.session.user){
		res.redirect("/err");
		res.session.err="您已经登录啦";
	}
	next();
}



module.exports = router;
